package com.tiancheng.trade.order.configuration;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * WebMvcAutoConfiguration
 */
@Configuration
@Order(3)
public class SpringMvcConfiguration implements WebMvcConfigurer {
    @Resource
    private WebMvcProperties mvcProperties;
	@Resource
	private WebProperties webProperties;

    @Autowired
    private UnderlineToCamelArgumentResolver underlineToCamelArgumentResolver;

    /**
     * 添加自定义的Converters和Formatters.
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 以下自定义的转换只适用于url传递参数
        registry.addConverter(new StringToLocalDateConverter());
        registry.addConverter(new StringToLocalDateTimeConverter());
    }

    /**
     * 因为拦截器或者过滤器的原因,导致spring.resourcesstatic-locations和spring.mvc.static-path-pattern配置无效，无法访问静态资源,需要重写次方法实现静态资源的访问
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(this.mvcProperties.getStaticPathPattern()).addResourceLocations(this.webProperties.getResources().getStaticLocations());
        //swagger相关
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(new ControllerInterceptor()).addPathPatterns("/**").order(-999);
    }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return (factory -> {
            ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/fe/index.html");
            factory.addErrorPages(errorPage404);
        });
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        //接收参数下划线转为驼峰
        argumentResolvers.add(underlineToCamelArgumentResolver);
    }

//
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        // 允许跨域
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
//                .allowCredentials(true).maxAge(3600);
//
//    }

}